/*
 * Copyright (C) 2009 AnK Team
 *	 Andrey Korolev
 *	 Anna Krel
 *	 Anna Kapanina
 *	 
 *	 http://code.google.com/p/ankfood/	
 *    
     
 *    Licensed under the GNU General Public License, Version 2.0 (the "License");
 *
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *         http://www.gnu.org/licenses/gpl-2.0.html

 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,

 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */



package org.ank.food;



import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class ProductDataProvider extends ContentProvider{
	
	public static final String DATABASE_LOCATION = "/data/data/org.ank.food/databases/";
	public static final String AUTHORITY = "org.ank.food.productdataprovider";
	
	public static final String DATABASE_NAME = "products.db";
	public static final int DATABASE_VERSION = 1;
	
	
	
	
	//products section
	public static final Uri CONTENT_URI_PRODUCTS = Uri.parse("content://" + AUTHORITY + "/products");
	public static final String TABLE_NAME_PRODUCTS = "products";
	public static final String[] COLUMN_NAMES_PRODUCTS = {"_id","group_id","name","prot_energy","fat_energy",
    	"carbo_energy","prot_content","fat_content", "carbo_content", "energy", "subgroup"};
	public static final int ID = 0;
	public static final int GROUP_ID = 1;
	public static final int NAME = 2;
	public static final int PROT_ENERGY = 3;
	public static final int FAT_ENERGY = 4;
	public static final int CARBO_ENERGY = 5;
	public static final int PROT_CONTENT = 6;
    public static final int FAT_CONTENT = 7;
    public static final int CARBO_CONTENT = 8;
    public static final int ENERGY = 9;
    public static final int SUBGROUP = 10;
	public static final String[] COLUMN_TYPES = {"integer prymary key","integer","text",
		"integer","integer","integer","float","float","float","float","text"};
	
	//groups section
	public static final String TABLE_NAME_GROUPS = "groups";
	public static final Uri CONTENT_URI_GROUPS = Uri.parse("content://" + AUTHORITY + "/groups");
	public static final String[] COLUMN_NAMES_GROUPS = {"_id","group_name"};
	public static final int GROUP_NAME = 1;
	
	//subgroups section
	public static final String TABLE_NAME_SUBGROUPS = "subgroups";
	public static final Uri CONTENT_URI_SUBGROUPS = Uri.parse("content://" + AUTHORITY + "/subgroups");
	public static final String[] COLUMN_NAMES_SUBGROUPS = {"_id","group_id",
		"subgroup_name"};
	public static final int GROUP_ID_SUBGROUPS = 1;
	public static final int SUBGROUP_NAME_SUBGROUPS = 2;
	
	//weight section
	public static final Uri CONTENT_URI_WEIGHT = Uri.parse("content://" + AUTHORITY + "/weight");
	public static final String TABLE_NAME_WEIGHT = "weight";
	public static final String[] COLUMN_NAMES_WEIGHT = {"_id","prod_id","amount","name","weight"};
	public static final int WEIGHT_PROD_ID = 1;
	public static final int WEIGHT_AMOUNT = 2;
	public static final int WEIGHT_NAME = 3;
	public static final int WEIGHT_WEIGHT = 4;
	
	
	
	//constants for uri matching
	
	public static final int IDENTIFIER_PRODUCTS_URI = 0;
	public static final int IDENTIFIER_GROUPS_URI = 1;
	public static final int IDENTIFIER_SUBGROUPS_URI = 2;
	public static final int IDENTIFIER_WEIGHT_URI = 3;
	
	
	public static final int MAX_GROUP_ID = 20;
	
	private static final UriMatcher sUriMatcher;
	
	public static class DatabaseHelper extends SQLiteOpenHelper {
		 
		public DatabaseHelper(Context context) {
			 super(context, DATABASE_NAME, null, DATABASE_VERSION);
		 }

		 @Override
		 public void onCreate(SQLiteDatabase db) {
			 StringBuffer buf = new StringBuffer("CREATE TABLE ");
			 buf.append(TABLE_NAME_PRODUCTS).append("(");
			 for( int i = 0; i < COLUMN_NAMES_PRODUCTS.length-1; i++ ){
				 buf.append(COLUMN_NAMES_PRODUCTS[i]).append(' ').append(COLUMN_TYPES[i]).append(", ");
			 }
			 buf.append(COLUMN_NAMES_PRODUCTS[COLUMN_NAMES_PRODUCTS.length-1]).append(' ').
			 	append(COLUMN_TYPES[COLUMN_TYPES.length-1]).append(");");
			 db.execSQL( buf.toString() );
		 }

		 @Override
		 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//			 db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_PRODUCTS+";");
//	         onCreate(db);
		 }
	 }
	
	 private DatabaseHelper mOpenHelper;
	
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext());
        return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch(sUriMatcher.match(uri)){
		case IDENTIFIER_GROUPS_URI :
			qb.setTables(TABLE_NAME_GROUPS);
			break;
		case IDENTIFIER_SUBGROUPS_URI :
			qb.setTables(TABLE_NAME_SUBGROUPS);
			break;
		case IDENTIFIER_PRODUCTS_URI :
			qb.setTables(TABLE_NAME_PRODUCTS);
			break;
		case IDENTIFIER_WEIGHT_URI :
			qb.setTables(TABLE_NAME_WEIGHT);
			break;
		}
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c; 
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 
		int a = db.update(TABLE_NAME_PRODUCTS, values, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return a;
	}
	
	static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        
        sUriMatcher.addURI(AUTHORITY, "products", IDENTIFIER_PRODUCTS_URI);
        sUriMatcher.addURI(AUTHORITY, "subgroups", IDENTIFIER_SUBGROUPS_URI);
        sUriMatcher.addURI(AUTHORITY, "groups", IDENTIFIER_GROUPS_URI);
        sUriMatcher.addURI(AUTHORITY, "weight", IDENTIFIER_WEIGHT_URI);
	}

}
